/**
 * all things facebook related are somewhere in here
 */

// import ResultCollection;
import com.adobe.serialization.json.JSON;
import com.facebook.graph.FacebookDesktop;
import com.facebook.graph.net.*;

import easysearch.ResultCollection;

import mx.collections.ArrayCollection;
import mx.managers.CursorManager;

private const appID:String = "229721973763471";
public var perms:Array = ['publish_stream','create_event','rsvp_event','sms','offline_access','email','read_insights',
	'read_stream','user_about_me',
	'user_activities','user_birthday','user_education_history','user_events','user_groups','user_hometown',
	'user_interests','user_likes','user_location','user_notes','user_online_presence',
	'user_photo_video_tags','user_photos','user_relationships','user_religion_politics','user_status',
	'user_videos','user_website','user_work_history','read_friendlists','read_requests'];
public var returnSession:Object;
public var accesstoken;
var peopleResults:Object;
var pagesResults:Object;
var eventsResults:Object;
var groupsResults:Object;
var basicLimit:String = "15";
var fullLimit:String = "50";

var peopleFields:String = "id,name,location,birthday";
var groupFields:String = "id,name,owner,icon";
var pageFields:String = "id,name,category,likes";
var eventFields:String = "id,name,description,venue,owner";

/**
 * facebook init and login/out handlers
 */

function init():void {
	FacebookDesktop.init(appID,loginHandler);
}

// LOGIN
function login():void {
	
	FacebookDesktop.login(loginHandler,perms);
}

function loginHandler(session:Object, fail:Object):void {
	if (session) {
		status="logged in";
		returnSession = session;
		accesstoken = session.accessToken;
		
		// if something is missing from the session i need to logout and try to clear it
		if(!session.expireDate) {
			FacebookDesktop.logout();
		}
	}
	else {
		status="logged out";
	}
}

// LOGOUT
function logout():void {
	FacebookDesktop.logout(logoutHandler);
}

function logoutHandler(logoutReturn:Object):void {
	
	status = "logged out";
}

/**
 * END facebook init and login/out handlers
 */


/**
 * BEGIN button handlers
 */

// basic search handler
function basicSearch_clickHandler(event:MouseEvent):void
{
	trace("start executing queries");
	// execute the basic search with no filters. this should be 4 api calls
	var peopleQuery:String = "/search?q="+query.text+"&type=user&"+fullLimit+"&access_token="+accesstoken+"&fields="+peopleFields;
	var pagesQuery:String = "/search?q="+query.text+"&type=page&"+fullLimit+"&access_token="+accesstoken+"&fields="+pageFields;
	var groupsQuery:String = "/search?q="+query.text+"&type=group&"+fullLimit+"&access_token="+accesstoken+"&fields="+groupFields;
	var eventsQuery:String = "/search?q="+query.text+"&type=event&"+fullLimit+"&access_token="+accesstoken+"&fields="+eventFields;
	
	FacebookDesktop.api(peopleQuery,handlePeopleResults,null,"GET");
	FacebookDesktop.api(pagesQuery,handlePagesResults,null,"GET");
	FacebookDesktop.api(groupsQuery,handleGroupsResults,null,"GET");
	FacebookDesktop.api(eventsQuery,handleEventsResults,null,"GET");
	
	var collectResults:ResultCollection = new ResultCollection();
	// in each search, we need to store the results in a new Result Collection
	
	if(peopleResults) {
		for (var i:int=0;i<peopleResults.length;i++) {
			trace("people entry: "+i);
			collectResults.addResultToAppropriateCollection("user",peopleResults[i]);
		}
	}
	
	if(pagesResults) {
		for (var j:int=0;j<pagesResults.length;j++) {
			trace("page entry: "+j);
			collectResults.addResultToAppropriateCollection("page",pagesResults[j]);
		}
	}
	
	if(groupsResults) {
		for (var k:int=0;k<groupsResults.length;k++) {
			trace("groups entry: "+k);
			collectResults.addResultToAppropriateCollection("user",groupsResults[k]);
		}
	}
	
	if(eventsResults) {
		for (var l:int=0;l<eventsResults.length;l++) {
			trace("events entry: "+l);
			collectResults.addResultToAppropriateCollection("event",eventsResults[l]);
		}
	}
	trace("done!");
	
}

/**
 * END button handlers
 */


/**
 * BEGIN api call handlers
 */

function handlePeopleResults(results:Object, fail:Object):void {
	peopleResults = results;
	trace("done handling users");
	
	
}
function handlePagesResults(results:Object, fail:Object):void {
	pagesResults = results;
	trace("done handling pages");
}
function handleGroupsResults(results:Object, fail:Object):void {
	groupsResults = results;
	trace("done handling groups");
}
function handleEventsResults(results:Object, fail:Object):void {
	eventsResults = results;
	trace("done handling events");
}

/**
 * END api call handlers
 */
